import { Injectable } from '@angular/core';
import { TreeNode } from 'primeng/primeng';
import { CudTableService } from '../cud-table/cud-table.service';

@Injectable()
export class TreeService {

    constructor(private treeService: CudTableService) { }

    getParentInfos(url, id, name) {
        return this.treeService
            .getRequest(url)
            .then((res: any) => {
                if (res.result_code != '0000') {
                    throw new Error(res.message);
                } else {
                    //console.log(res.result)
                    return this.processTreeData(res.result, id, name);
                }
            })
            .catch((err: any) => {
                console.error('请检查您的网络！');
            });
    }

    postParentInfos(url,data,id, name) {
        return this.treeService
            .postRequest(url,data)
            .then((res: any) => {
                if (res.result_code != '0000') {
                    throw new Error(res.message);
                } else {
                    //console.log(res.result)
                    return this.processTreeDataNew(res.result,id, name);
                }
            })
            .catch((err: any) => {
                console.error('请检查您的网络！');
            });
    }

    public processTreeData(list: Array<any>, id, name): Array<any> {
        return this.getTreeChildren(list, '0', id, name);
    }

    public processTreeDataNew(list: Array<any>, id, name): Array<any> {
        return this.getTreeChildrenNew(list, 0, id, name,'parentid');
    }

    public getTreeChildrenNew(list: Array<any>, parentId: number, id?: string, name?: string,parentKey?:string): Array<any> {
        let idKey: string = id;
        let labelKey: string = name;
        let result: Array<any> = [];
        for (let item of list) {
            if (item[parentKey] === parentId) {
                this.fillCSTreeNode(item, item[idKey], item[labelKey]);
                let children = this.getTreeChildrenNew(list, item[idKey], id, name,parentKey);
                if (children && children.length > 0) {
                    item.children = children;
                    item.leaf = false;
                } else {
                    item.leaf = true;
                }
                result.push(item);
            }
        }
        return result;
    }

    public getTreeChildren(list: Array<any>, parentId: string, id?: string, name?: string): Array<any> {
        let idKey: string = id;
        let labelKey: string = name;
        let result: Array<any> = [];
        for (let item of list) {
            if (item.parentId === parentId) {
                this.fillCSTreeNode(item, item[idKey], item[labelKey]);
                let children = this.getTreeChildren(list, item[idKey], id, name);
                if (children && children.length > 0) {
                    item.children = children;
                    item.leaf = false;
                } else {
                    item.leaf = true;
                }
                result.push(item);
            }
        }
        return result;
    }

    public fillCSTreeNode(node: any, data: string, label: string): void {
        node.data = data;
        node.label = label;
        node.expandedIcon = 'fa-minus-square-o';
        node.collapsedIcon = 'fa-plus-square-o';
        node.expanded = false;
        //node.leaf = true;
        //node.icon = 'fa-dot-circle-o';
    }
    public getTreeData({
        url,
        idName,
        parentIdName,
        topId,
        labelName,
        initNode = (node) => node,
        method
    }: {
            url: string,
            idName: string,
            parentIdName: string,
            topId: string,
            labelName: string,
            initNode?: Function,
            method?: string
        }) {
        let toTree = (list: Array<any>, topId = '0', idName, parentIdName, labelName) => {
            let idKey = idName;
            let labelKey = labelName;
            let result = [];
            for (let item of list) {
                if (item[parentIdName] == topId) {
                    this.fillCSTreeNode(item, item[idKey], item[labelKey]);
                    item = initNode(item)
                    let children = toTree(list, item[idKey], idName, parentIdName, labelName)
                    if (children && children.length > 0) {
                        item.children = children;
                        item.leaf = false;
                    } else {
                        item.leaf = true;
                    }
                    result.push(item);
                }
            }
            return result;
        }
        return this.treeService.getRequest(url).then(res => {
            if (res.result_code != '0000') {
                throw new Error(res.message);
            } else {
                return toTree(res.result, topId, idName, parentIdName, labelName);
            }
        })
            .catch((err: any) => {
                console.error('请检查您的网络！');
            });
    }

    public postTreeData({
        url,
        idName,
        parentIdName,
        topId,
        labelName,
        initNode = (node) => node,
        data
    }: {
            url: string,
            idName: string,
            parentIdName: string,
            topId: string,
            labelName: string,
            initNode?: Function,
            data?: any
        }) {
        let toTree = (list: Array<any>, topId = '0', idName, parentIdName, labelName) => {
            let idKey = idName;
            let labelKey = labelName;
            let result = [];
            for (let item of list) {
                if (item[parentIdName] == topId) {
                    this.fillCSTreeNode(item, item[idKey], item[labelKey]);
                    item = initNode(item)
                    let children = toTree(list, item[idKey], idName, parentIdName, labelName)
                    if (children && children.length > 0) {
                        item.children = children;
                        item.leaf = false;
                    } else {
                        item.leaf = true;
                    }
                    result.push(item);
                }
            }
            return result;
        }
        return this.treeService.postRequest(url,data).then(res => {
            if (res.result_code != '0000') {
                throw new Error(res.message);
            } else {
                return toTree(res.result, topId, idName, parentIdName, labelName);
            }
        })
            .catch((err: any) => {
                console.error('请检查您的网络！');
            });
    }

}
